<?php

class Connection
{

	private $m_bConnected = false;

	private $m_sName;
	private $m_sServer;
	private $m_iPort;
	
	private $m_sBind;
	
	private $m_oSock;
	
	private $m_aInfo;
	
	public function __construct( $aInfo, $sName, $sServer, $iPort = 6667, $sBind = null )
	{

		$this -> m_sName 	= $sName;
		$this -> m_sServer 	= $sServer;
		$this -> m_iPort 	= $iPort;
		$this -> m_sBind 	= $sBind;


		$this -> m_aInfo 	= $aInfo;


		$this -> m_bConnected= false;

		$this -> m_oSock = @ socket_create( AF_INET, SOCK_STREAM, SOL_TCP );

		if ( $sBind != null )
		{
			$res = @ socket_bind( $this -> m_oSock, $sBind );
			if ( ! $res )
			{
				// LogFile
				return false;
			}

		}

	} // end __construct function
	
	public function __destruct( )
	{
		$this -> Disconnect( );
	}
	
	public function Connect( ) 
	{
		if ( $this -> m_bConnected == true )
			return false;

		$res = @ socket_connect( $this -> m_oSock, $this -> m_sServer, $this -> m_iPort );
		if ( ! $res ) {
			//Timers :: obj( ) -> addTimer( );
			echo socket_strerror (socket_last_error ($this -> m_oSock)) . chr(10);
			return false;
		}

		$res = socket_set_nonblock( $this -> m_oSock );
		if ( ! $res ) {
			// LogFile
			return false;
		}
		$this -> m_bConnected = true;
		$sNick 	= $this -> m_aInfo[ 'nick'];
		$sIdent 	= $this -> m_aInfo[ 'ident'];
		$sReal 	= $this -> m_aInfo[ 'real'];

		$this -> raw ( 'USER ' . $sIdent . ' 8 * :' . $sReal );
		$this -> raw ( 'NICK ' . $sNick );
		
		return true;
	}
	
	public function Killed( )
	{
		$this -> m_bConnected = false;
		socket_close( $this -> m_oSock );
		$this -> m_oSock = @ socket_create( AF_INET, SOCK_STREAM, SOL_TCP );
	}

	public function Disconnect( $sQuitMsg = VERSION_STR )
	{
		if ( ! $this -> m_bConnected )
			return false;
		
		$this -> raw( 'QUIT ' . $sQuitMsg );
		socket_close( $this -> m_oSock );
		$this -> m_oSock = @ socket_create( AF_INET, SOCK_STREAM, SOL_TCP );
		$this -> m_bConnected = false;
	}

	public function Check( ) 
	{
		$sRead = trim( @ socket_read ( $this -> m_oSock, 65000 ) );
		if ( $sRead === false )
		{
			// LogFile
			// Timers :: obj( ) -> addTimer( );
			echo socket_strerror( socket_last_error( $this -> m_oSock ) ) . "\n";
			return false;
		}
		
		if ( strlen( $sRead ) == 0 )
			return false;

		$aLines = explode( "\n", trim( $sRead ) );
		
		foreach( $aLines as $sLine )
		{
			$sLine = trim( $sLine );
			if ( strlen( $sLine ) == 0 )
				continue;

			$oBot = Bots :: obj( ) -> getBot( $this -> m_aInfo[ 'nick'] );
			$oBot -> bufferIn -> Network = $this -> m_sName;
			$oBot -> process( $sLine );
		}
	}
	
	public function raw( $msg ) 
	{
		if ( ! $this -> m_bConnected )
			return false;
		//echo '=> '.$msg.chr(10);
		$res = socket_write( $this -> m_oSock, $msg . "\r\n" );
		if ( ! $res ) {
			// LogFile
			// Timers :: obj( ) -> addTimer( );
			echo socket_strerror( socket_last_error( $this -> m_oSock ) ) . "\n";
			return false;
		}
		return true;
	}
	
	public function msg( $sTo, $sMsg )
	{
		$this -> raw( 'PRIVMSG ' . $sTo . ' :' . $sMsg );
	}
	
	public function notice( $sTo, $sMsg )
	{
		$this -> raw( 'NOTICE ' . $sTo . ' :' . $sMsg );
	}
	
	public function mode( $sWhere, $sMode, $sParam )
	{
		$this -> raw
		(
			'MODE ' . $sWhere . ' ' . $sMode . ' ' . $sMsg
		);
	}
	
	public function __toString( )
	{
		return 'Connection class';
	}
	
	public function getSock( ) 
	{
		return $this -> m_oSock;
	}

} // end Connection Class

?>